package czh.cocoa.ui.impl;

import czh.cocoa.animation.CALayer;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSet;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGColor;
import czh.cocoa.graphics.CGContext;
import czh.cocoa.graphics.CGGradient;
import czh.cocoa.graphics.CGPath;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIEvent;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIKit;
import czh.cocoa.ui.UIResponder;
import czh.cocoa.ui.UITextAlignment;
import czh.cocoa.ui.UITouch;
import czh.cocoa.ui.UIView;

import static czh.cocoa.ui.impl.UIControl.*;
import static czh.cocoa.graphics.CGContext.*;

public class UITableViewCell extends UIView {
	
	public final static int UITableViewCellStyleDefault = 0;
	public final static int UITableViewCellStyleValue1 = 1;
	public final static int UITableViewCellStyleValue2 = 2;
	public final static int UITableViewCellStyleSubtitle = 3;
	public final static int UITableViewCellStyleCustom = 4;  //additional

	public final static int UITableViewCellSeparatorStyleNone = 0;
	public final static int UITableViewCellSeparatorStyleSingleLine = 1;
	public final static int UITableViewCellSeparatorStyleSingleLineEtched = 2;
	
	public final static int UITableViewCellAccessoryNone = 0;
	public final static int UITableViewCellAccessoryDisclosureIndicator = 1;
	public final static int UITableViewCellAccessoryDetailDisclosureButton = 2;
	public final static int UITableViewCellAccessoryCheckmark = 3;
	
	public final static int UITableViewCellSelectionStyleNone = 0;
	public final static int UITableViewCellSelectionStyleBlue = 1;
	public final static int UITableViewCellSelectionStyleGray = 2;
	
	final static int UITableViewCellGroupedNone = 0;
	final static int UITableViewCellGroupedWhole = 1;
	final static int UITableViewCellGroupedTop = 2;
	final static int UITableViewCellGroupedMiddle = 3;
	final static int UITableViewCellGroupedBottom = 4;
	
	final static float DEFAULT_TEXT_INDENTATION = 10;
	
	private int mStyle;
	
	private NSString mReuseIdentifier;
	
	private UIView mSeparatorView;
	
	private UIView mContentView;
	
	private UIImageView mImageView;
	
	private UILabel mTextLabel;
	
	private UILabel mDetailTextLabel;
	
	private int mIndentationLevel;
	
	private float mIndentationWidth;
	
	private int mAccessoryType;
	
	private UIView mAccessoryView;
	
	private int mSelectionStyle;
	
	private UIView mSelectionView;
	
	private boolean mSelected;
	
	private UIView mBackgroundView;
	
	private UIView mSelectedBackgroundView;
	
	private float mGroupedPadding;
	
	private int mGroupedBackgroundStyle;
	
	public UITableViewCell() {
		this(UITableViewCellStyleDefault, null); 
	}
	
	public UITableViewCell(int style, NSString reuseIdentifier) {
		mReuseIdentifier = reuseIdentifier;
		setStyle(style);
	}
	
	protected void init() {
		super.init();
		mIndentationWidth = 10;
		mSelectionStyle = UITableViewCellSelectionStyleBlue;
		
		CGRect bounds = bounds();
		mSeparatorView = new UIView(CGRectMake(0, bounds.size.height - 1, bounds.size.width, 1));
		mSeparatorView.setAutoresizingMask(UIViewAutoresizingFlexibleWidth | 
						UIViewAutoresizingFlexibleTopMargin);
		addSubview(mSeparatorView);
		
		mContentView = new UIView(CGRectMake(0, 0, bounds.size.width, bounds.size.height - 1));
		//mContentView.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		addSubview(mContentView); 
	}
	
	private void setStyle(int style) {
		mStyle = style;
		
		if (style != UITableViewCellStyleCustom) {
			
			CGRect bounds = mContentView.bounds();
			
			float indentation = mIndentationWidth * mIndentationLevel + DEFAULT_TEXT_INDENTATION;
			
			mTextLabel = new UILabel(CGRectChange(bounds, indentation, 0, -indentation, 0)); 
			mTextLabel.setFont(UIFont.boldSystemFontOfSize(20));
			//mTextLabel.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
			mContentView.addSubview(mTextLabel);
			
			//if (style != UITableViewCellStyleValue2) {
				indentation = mIndentationWidth * mIndentationLevel;
				mImageView = new UIImageView(CGRectMake(indentation, 0, 0, bounds.size.height));  
				//mImageView.setAutoresizingMask(UIViewAutoresizingFlexibleHeight);
				mContentView.addSubview(mImageView);
			//}
			
			if(style != UITableViewCellStyleDefault) {
				mDetailTextLabel = new UILabel(CGRectChange(mTextLabel.frame(),0,0,-DEFAULT_TEXT_INDENTATION,0));
				mDetailTextLabel.setTextColor(new UIColor(0.22f,0.33f,0.53f,1));
				//mDetailTextLabel.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
				mDetailTextLabel.setTextAlignment(UITextAlignment.Right);
				mContentView.addSubview(mDetailTextLabel);
			}
			
		}
		
	}
	
	public NSString reuseIdentifier() {
		return mReuseIdentifier;
	}
	
	void setSeparatorStyleWithColor(int style, UIColor color) {
		switch (style) {
		case UITableViewCellSeparatorStyleNone:
		case UITableViewCellSeparatorStyleSingleLineEtched:
			mSeparatorView.setHidden(true);
			break;
		case UITableViewCellSeparatorStyleSingleLine:
			mSeparatorView.setHidden(false);
			mSeparatorView.setBackgroundColor(color);
			bringSubviewToFront(mSeparatorView);
			break;
		default:
			break;
		}
	}
	
	public UIView contentView() {
		return mContentView;
	}
	
	public UILabel textLabel() {
		return mTextLabel;
	}
	
	public UILabel detailTextLabel() {
		return mDetailTextLabel;
	}
	
	public UIImageView imageView() {
		return mImageView;
	}
	
	void layoutSubviewsInCell() {
		
		CGRect bounds = bounds();
		
		if (bounds.size.width <= 0 || bounds.size.height <= 0) {
			return;
		}
		
		if (mAccessoryView != null) {
			CGRect accessory_frame = mAccessoryView.frame();
			float margin = accessory_frame.size.width + DEFAULT_TEXT_INDENTATION + mGroupedPadding;
			
			accessory_frame.origin.x = bounds.size.width - margin;
			accessory_frame.origin.y = (bounds.size.height - accessory_frame.size.height) / 2;
			mAccessoryView.setFrame(accessory_frame);
			
			bounds.size.width -= margin;
		}
		
		bounds.origin.x = mGroupedPadding;
		bounds.size.width -= mGroupedPadding;
		
		mContentView.setFrame(bounds);
		
//		UIView subview = (UIView) mContentView.subviews().objectAtIndex(0);
//		NSLog("content frame: " + bounds + "  " + subview.frame()); 
		
		if (mStyle == UITableViewCellStyleCustom) { 
			return;
		}
		
		float indentation = mIndentationWidth * mIndentationLevel;
		
		UIImage image = mImageView.image();
		
		if (image != null) {
			mImageView.setHidden(false);
			
			CGSize img_size = image.size();
			
			float img_view_height = bounds.size.height;
			float img_view_width = img_view_height * (img_size.width / img_size.height);
			
			mImageView.setFrame(CGRectMake(indentation, 0, img_view_width, img_view_height)); 
			
			if (img_view_width > img_size.width) {
				mImageView.setContentMode(UIViewContentModeCenter);
			} else {
				mImageView.setContentMode(UIViewContentModeScaleToFill);
			}	
			
			indentation += img_view_width;
			
		} else {
			mImageView.setHidden(true);
		}
		
		indentation += DEFAULT_TEXT_INDENTATION;
		
		CGSize constrained_size = CGSizeMake(bounds.size.width - indentation - 
								  DEFAULT_TEXT_INDENTATION, bounds.size.height);
		
		CGSize text_size = mTextLabel.sizeThatFits(constrained_size);
		
		//mTextLabel.setBackgroundColor(UIColor.yellowColor());
		
		switch (mStyle) {
		case UITableViewCellStyleDefault:
		{
			mTextLabel.setFrame(CGRectMake(indentation, (bounds.size.height - text_size.height)/2, 
					text_size.width, text_size.height)); 
		}
			break;
		case UITableViewCellStyleValue1:
		case UITableViewCellStyleValue2:
		{
			mTextLabel.setFrame(CGRectMake(indentation, (bounds.size.height - text_size.height)/2, 
					text_size.width, text_size.height)); 
			
			constrained_size.width -= mTextLabel.frame().size.width;
			if(constrained_size.width < 0) constrained_size.width = 0;
			CGSize text_size2 = mDetailTextLabel.sizeThatFits(constrained_size);
			mDetailTextLabel.setFrame(CGRectMake(bounds.size.width - DEFAULT_TEXT_INDENTATION - text_size2.width, 
					(bounds.size.height - text_size2.height)/2, 
					text_size2.width, text_size2.height)); 
			//mDetailTextLabel.setBackgroundColor(UIColor.redColor());
		}
			break;
		case UITableViewCellStyleSubtitle:
		{
			CGSize text_size2 = mDetailTextLabel.sizeThatFits(constrained_size);
			float offy = (bounds.size.height - text_size.height - text_size2.height) / 2;
			mTextLabel.setFrame(CGRectMake(indentation, offy, text_size.width, text_size.height));
			mDetailTextLabel.setFrame(CGRectMake(indentation, offy + text_size.height, text_size2.width, text_size2.height));
		}
			break;
		}

	}
	
	public void setIndentationWidth(float width) {
		mIndentationWidth = width;
	}
	
	public float indentationWidth() {
		return mIndentationWidth;
	}
	
	public void setIndentationLevel(int level) {
		mIndentationLevel = level;
	}
	
	public int indentationLevel() {
		return mIndentationLevel;
	}
	
	public void setAccessoryType(int type) {
		if (mAccessoryType != type) {
			mAccessoryType = type;
			
			if (mAccessoryView != null) {
				mAccessoryView.removeFromSuperview();
				mAccessoryView = null;
			}
			
			UIButton button = new UIButton();
			UIImage button_image = null;
			UIImage button_image_highlighted = null;
			
			switch(type){
			case UITableViewCellAccessoryDisclosureIndicator:
				button_image = UIImage.imageNamed(NSTR("cocoa/accessory_indicator"));
				button_image_highlighted = UIImage.imageNamed(NSTR("cocoa/accessory_indicator_highlighted"));
				break;
			case UITableViewCellAccessoryDetailDisclosureButton:
				button_image = UIImage.imageNamed(NSTR("cocoa/accessory_disclosure"));
				button_image_highlighted = UIImage.imageNamed(NSTR("cocoa/accessory_disclosure_highlighted"));
				break;
			case UITableViewCellAccessoryCheckmark:
				button_image = UIImage.imageNamed(NSTR("cocoa/accessory_checkmark"));
				button_image_highlighted = UIImage.imageNamed(NSTR("cocoa/accessory_checkmark_highlighted"));
				break;
			}
			
			button.setImageForState(button_image, UIControlStateNormal);
			button.setImageForState(button_image_highlighted, UIControlStateHighlighted);
			
			CGSize button_image_size = button_image.size();
			button.setFrame(CGRectMake(0, 0, button_image_size.width, button_image_size.height));
			
			mAccessoryView = button;
			addSubview(mAccessoryView);
		}
	}
	
	public int accessoryType() {
		return mAccessoryType;
	}
	
	public void setAccessoryView(UIView view) {
		setAccessoryType(UITableViewCellSeparatorStyleNone);
		mAccessoryView = view;
		addSubview(mAccessoryView);
	}
	
	public UIView accessoryView() {
		return mAccessoryView;
	}
	
	public boolean isSelected() {
		return mSelected;
	}
	
	public void setSelected(boolean selected) {
		if (mSelected != selected) { 
			mSelected = selected;
			
			if (mSelectionStyle == UITableViewCellSelectionStyleNone) { 
				return;
			}
			
			if (selected) {
				if (mSelectedBackgroundView != null) { 
					mSelectionView = mSelectedBackgroundView;
				} else {
					mSelectionView = new UITableCellSelectionView(); 
				}
				
				CGRect bounds = bounds();
				bounds.origin.x += mGroupedPadding;
				bounds.size.width -= mGroupedPadding * 2;
				mSelectionView.setFrame(bounds);
				
				if (mBackgroundView != null) {
					insertSubviewAboveSubview(mSelectionView, mBackgroundView); 
				} else {
					insertSubviewAtIndex(mSelectionView, 0);
				}
				
				for (NSObject o : mContentView.subviews()) {
					if (o instanceof UILabel) {
						UILabel label = (UILabel) o;
						label.setHighlighted(true);
					}
				}
				
			} else {
				mSelectionView.removeFromSuperview();
				mSelectionView = null;
				
				for (NSObject o : mContentView.subviews()) {
					if (o instanceof UILabel) {
						UILabel label = (UILabel) o;
						label.setHighlighted(false);
					}
				}
			}
		}
	}
	
	public void setBackgroundView(UIView view) {
		if (mBackgroundView != null) { 
			mBackgroundView.removeFromSuperview();
		}
		if (view != null) {
			CGRect bounds = bounds();
			bounds.origin.x += mGroupedPadding;
			bounds.size.width -= mGroupedPadding * 2;
			
			view.setFrame(bounds); 
			view.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
			insertSubviewAtIndex(view, 0);
		}
		mBackgroundView = view;
	}
	
	public UIView backgroundView() {
		return mBackgroundView;
	}
	
	public void setSelectedBackgroundView(UIView view) {
		mSelectedBackgroundView = view;
	}
	
	public UIView selectedBackgroundView() {
		return mSelectedBackgroundView;
	}
	
	public void setSelectionStyle(int style) {
		mSelectionStyle = style;
	}
	
	public int selectionStyle() {
		return mSelectionStyle;
	}
	
	void setGroupedPadding(float padding) {
		mGroupedPadding = padding;
	}
	
	void setGroupedBackgroundStyle(int style) {
		
		mGroupedBackgroundStyle = style;
		
		UIImageView backgrond = new UIImageView();
		UIImage image = null;
		
		switch (style) {
		case UITableViewCellGroupedWhole: 
			image = UIImage.imageNamed(NSTR("cocoa/table_group_cell_background"));
			backgrond.setImage(image);
			backgrond.setContentStretch(CGRectMake(0.5f, 0.5f, 0.1f, 0.1f));
			break;
		case UITableViewCellGroupedTop: 
			image = UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_top"));
			backgrond.setImage(image);
			backgrond.setContentStretch(CGRectMake(0.5f, 0.7f, 0.1f, 0.1f));
			break;
		case UITableViewCellGroupedBottom: 
			image = UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_bottom"));
			backgrond.setImage(image);
			backgrond.setContentStretch(CGRectMake(0.5f, 0.2f, 0.1f, 0.1f));
			break;
		case UITableViewCellGroupedMiddle: 
			image = UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_middle"));
			backgrond.setImage(image);
			backgrond.setContentStretch(CGRectMake(0.5f, 0.5f, 0.1f, 0.1f));
			break;
		default:
			backgrond = null;
		}
		
		setBackgroundView(backgrond); 
	}
	
	private class UITableCellSelectionView extends UIView {
		
		public UITableCellSelectionView() {
			
		}
		
		public void drawRect(CGRect rect) {
			CGColor color, color2;
			if (mSelectionStyle == UITableViewCellSelectionStyleBlue) { 
				color = new CGColor(new float[]{5/255f, 140/255f, 245/255f});
				color2 = new CGColor(new float[]{1/255f, 94/255f, 231/255f});
			} else {
				color = new CGColor(new float[]{205/255f, 205/255f, 205/255f});
				color2 = new CGColor(new float[]{170/255f, 170/255f, 170/255f});
			}
			
			CGContext ctx = UIKit.UIGraphicsGetCurrentContext(); 
			CGContextSaveGSState(ctx);
			
			CGPath path = null;
			
			switch (mGroupedBackgroundStyle) {
			case UITableViewCellGroupedWhole: 
				path = CGPath.roundRectPath(rect, 15, 15, 15, 15);
				break;
			case UITableViewCellGroupedTop: 
				path = CGPath.roundRectPath(rect, 15, 15, 0, 0);
				break;
			case UITableViewCellGroupedBottom: 
				path = CGPath.roundRectPath(rect, 0, 0, 15, 15);
				break;
			}
			
			if (path != null) { 
				CGContextBeginPath(ctx);
				CGContextAddPath(ctx, path);
				CGContextClip(ctx);
			}
			
			CGGradient gradient = new CGGradient(new CGColor[]{color,color2}, null); 
			CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, rect.size.height), 0);
		
			CGContextRestoreGSState(ctx);
		}
		
	}
	
}
